package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Override
    @Transactional //保证数据的一致性
    public void insert(DishDto dishDto) {
        //保存菜品数据
        dishMapper.insert(dishDto);
        List<DishFlavor> flavors = dishDto.getFlavors();

        //判断是否选择了口味
        if (flavors != null && flavors.size() > 0) {
            Long id = dishDto.getId(); //菜品id
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(id);
                dishFlavorMapper.insert(flavor);
            }
        }

    }
}
